package com.cykj.pos.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.pos.domain.BizCashoutRecords;
import com.cykj.pos.domain.BizMerchant;
import com.cykj.pos.mapper.BizPosMachineMapper;
import com.cykj.pos.service.IBizCashoutRecordsService;
import com.cykj.pos.service.IBizWalletService;
import com.cykj.pos.service.PosV1AppMerApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/pos/v1/merApi")
@Api(tags = "代付Api")
public class PosV1AppMerApiController {

    private final PosV1AppMerApiService posV1AppMerApiService;
    private final IBizWalletService walletService;
    private final BizPosMachineMapper posMachineMapper;
    private final IBizCashoutRecordsService cashoutRecordsService;
    /**
     * 用户签约接口(userSign)（同步接口）
     * @param jsonObject
     * @return
     */
//    @ApiOperation("用户签约")
//    @PostMapping("/userSign")
//    public AjaxResult userSign(@RequestBody JSONObject jsonObject) {
//        Map<String, Object> map = posV1AppMerApiService.userSign(jsonObject);
//        JSONObject obj =JSONObject.parseObject(map.get("result").toString());
//        return AjaxResult.success(obj.get("businessStatus").toString(),obj.get("explain"));
//    }

    /**
     * 查询签约状态
     */
    @PostMapping("/getStatus")
    public AjaxResult getStatus(@RequestBody JSONObject jsonObject) {
        return AjaxResult.success("获取成功",posV1AppMerApiService.getStatus(jsonObject));
    }

    /**
     * 出款接口（batchPayment）
     * @param jsonObject
     * @return
     */
//    @ApiOperation("出款接口")
//    @PostMapping("/batchPayment")
//    public AjaxResult batchPayment(@RequestBody JSONObject jsonObject) throws Exception {
//        AjaxResult ajaxResult = posV1AppMerApiService.batchPayMent(jsonObject);
//        return ajaxResult;
//    }

    /**
     * 单笔出款结果查询接口（queryPayment）
     * @param jsonObject
     * @return
     */
    @ApiOperation("单笔出款结果查询")
    @PostMapping("/queryPayment")
    public AjaxResult queryPayment(@RequestBody JSONObject jsonObject) {
        Map<String, Object> map = posV1AppMerApiService.queryPayment(jsonObject);
        JSONObject obj =JSONObject.parseObject(map.get("result").toString());
        if(!"0000".equals(obj.get("resultCode"))){
            return AjaxResult.error("fail",obj.get("resultMsg"));
        }else{
            return AjaxResult.success("success",map);
        }
    }

    /**
     * 出款结果异步通知接口（notifyPayment）
     * @param
     * @return
     */
    @ApiOperation("大庆伙伴科技有限公司   出款结果异步通知接口")
    @PostMapping("/notifyPayment")
    public String notifyPayment(@RequestBody String requestJson) throws Exception {
        System.out.println("----------异步接口回调中-----------");
        if(requestJson != null){
            String code = "860161310602294";
            return posV1AppMerApiService.notifyPayment(requestJson,code);
        }else{
            return "fail";
        }
    }

    @ApiOperation("大庆伙伴科技有限公司 安心付-汇企云   出款结果异步通知接口")
    @PostMapping("/notifyPaymentHb")
    public String notifyPaymentHb(@RequestBody String requestJson) throws Exception {
        System.out.println("----------安心付-伙伴-汇企云异步接口回调中-----------");
        if(requestJson != null){
            String code = "860161310602294";
            return posV1AppMerApiService.notifyPaymentHb(requestJson,code);
        }else{
            return "fail";
        }
    }

    @ApiOperation("大庆市乐富科技有限公司  安心付-汇企云   出款结果异步通知接口")
    @PostMapping("/notifyPaymentLf")
    public String notifyPaymentLf(@RequestBody String requestJson) throws Exception {
        System.out.println("----------安心付-乐富-汇企云异步接口回调中-----------");
        if(requestJson != null){
            String code = "860161310602295";
            return posV1AppMerApiService.notifyPaymentLf(requestJson,code);
        }else{
            return "fail";
        }
    }

    /**
     * 出款结果异步通知接口（notifyPayment）
     * @param
     * @return
     */
    @ApiOperation("大庆市乐富科技有限公司  出款结果异步通知接口")
    @PostMapping("/notifyPaymentDQLF")
    public String notifyPaymentDQLF(@RequestBody String requestJson) throws Exception {
        System.out.println("----------异步接口回调中-----------");
        if(requestJson != null){
            String code = "860161310602295";
            return posV1AppMerApiService.notifyPayment(requestJson,code);
        }else{
            return "fail";
        }
    }
    @PostMapping("/with")
    public String with(@RequestBody JSONObject jsonObject){
        Long merchId=jsonObject.getLong("merchId");
        String batchNo=jsonObject.getString("batchNo");
        BizMerchant merchant1=posMachineMapper.getMerchantByMerchId(merchId);
        BizCashoutRecords cashoutRecords =
                cashoutRecordsService.getCashoutRecordsByCustOrderNo(batchNo);
        walletService.with(merchant1,cashoutRecords);
        return null;
    }


}
